﻿using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Administration.Claims;
using Microsoft.SharePoint.WebControls;
using System;
using System.Collections.Generic;
using System.IdentityModel.Claims;

namespace g2rcp
{
    public class Constants
    {
        public const string G2RCPPersistedObjectID = "66e17d2e-dba5-4441-a922-828ed6548bed";
    }

    public enum IdentityValueDisplayEnum { IdentityValue, SpecificValue, IncludeValueThatResolvedInput };

    //[GuidAttribute(Constants.G2RCPPersistedObjectID)]
    public class G2RCPConfig : SPPersistedObject
    {
        public const string G2RCPPersistecObjectName = "G2RCPConfig";

        [Persisted]
        public bool GroupsToRoles;

        [Persisted]
        public string cpn;
        
        [Persisted]
        public string cpt;

        [Persisted]
        public bool UseSharePointDomain = true;

        [Persisted]
        public string LDAPConnectionString;

        [Persisted]
        public string LDAPUserName;

        [Persisted]
        public string LDAPPassword;

        [Persisted]
        public string LDAPfilter;
        
        public G2RCPConfig(SPPersistedObject parent)
            : base(G2RCPPersistecObjectName, parent)
        {
        }

        public G2RCPConfig()
        {
        }

        protected override bool HasAdditionalUpdateAccess()
        {
            return false;
        }

        public static G2RCPConfig GetFromConfigDB
        {
            get
            {
                SPPersistedObject parent = SPFarm.Local;
                try
                {
                    return parent.GetChild<G2RCPConfig>(G2RCPPersistecObjectName);
                }
                catch (Exception ex)
                {
                    G2RCP.LogToULS(String.Format("Error while retrieving SPPersistedObject {0}: {1}", G2RCPPersistecObjectName, ex.Message), TraceSeverity.Unexpected, EventSeverity.Error);
                }
                return null;
            }
        }

        /// <summary>
        /// Create the persisted object that contains default configuration of G2RCP.
        /// It should be created only in central administration with application pool credentials
        /// because this is the only place where we are sure user has the permission to write in the config database
        /// </summary>
        public static G2RCPConfig CreatePersistedObject()
        {
            G2RCPConfig PersistedObject = new G2RCPConfig(SPFarm.Local);
            PersistedObject.Id = new Guid(Constants.G2RCPPersistedObjectID);
            PersistedObject = GetDefaultSettings(PersistedObject);
            PersistedObject.Update();

            G2RCP.LogToULS(
                String.Format("Created PersistedObject {0} with Id {1}", PersistedObject.Name, PersistedObject.Id),
                TraceSeverity.Medium,
                EventSeverity.Information);

            return PersistedObject;
        }

        public static G2RCPConfig GetDefaultSettings(G2RCPConfig PersistedObject)
        {
            PersistedObject.GroupsToRoles = false;
            PersistedObject.cpt = SPOriginalIssuerType.Unknown.ToString();
            PersistedObject.cpn = "to.be.set";
            PersistedObject.UseSharePointDomain = false;
            PersistedObject.LDAPConnectionString = "";
            PersistedObject.LDAPUserName = "username";
            PersistedObject.LDAPPassword = "password";
            PersistedObject.LDAPfilter = "(&(objectClass=group)(sAMAccountName=*{0}*))";
            return PersistedObject;
        }

    }
}
